#include <stdio.h>
#include "checksum.h"
#include "hdcrypt.h"

int main(int argc, char *argv[])
{
	unsigned int ret;
	uint8_t serial_no[HDCRYPT_SERIAL_NO_LEN];
	uint8_t mac[5],ch[100];
	uint8_t start_num;
	int i;
		
	if(argc < 3){
		printf("Usage:%s start_num eth0 eth1 ...\n",argv[0]);
		return -1;
	}
	
	start_num = atoi(argv[1]);
	if((start_num+argc-3)>255){
		printf("start_num overflow\n");
		return -1;
	}
	
	ret = hdcrypt_init();
	
	if (ret != HDCRYPT_SUCCESS)
	{
		printf("hdcrypt init failed,ret=%d\n",ret);
		return -1;
	}
	
	ret = hdcrypt_get_serial_no(serial_no);
	if (ret != HDCRYPT_SUCCESS)
	{
		printf("hdcrypt get serial no failed,ret=%d\n",ret);
		return -1;
	}
	//for(i=0;i<HDCRYPT_SERIAL_NO_LEN;i++)
	//	printf("%02X ",serial_no[i]);
	
	//printf("\n");
	
	ret = crc_32(serial_no,HDCRYPT_SERIAL_NO_LEN);
	//printf("serial_no--ret=%d  0x:%x\n",ret,ret);
	
	
	for (i=3;i>=0;i--)
	{
		mac[i]=ret&0xff;
		ret >>= 8;
	}
	for (i=2;i<argc;i++)
	{
		mac[4]=i-2+start_num;	
		sprintf(ch,"ifconfig %s down;ifconfig %s hw ether 00:%02x:%02x:%02x:%02x:%02x;ifconfig %s up\n",argv[i],argv[i],mac[0],mac[1],mac[2],mac[3],mac[4],argv[i]);
		//printf("%s\n",ch);
		system(ch);
	}
	hdcrypt_release();
	return 0;
	
}
